package com.liuxing.doctor.controller;

import com.liuxing.doctor.common.util.DateUtils;
import com.liuxing.doctor.dto.MedicalRecordDateDto;
import com.liuxing.doctor.entity.Doctor;
import com.liuxing.doctor.entity.MedicalRecord;
import com.liuxing.doctor.entity.Patient;
import com.liuxing.doctor.service.MedicalRecordService;
import com.liuxing.doctor.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 行柳
 * 学习使用
 */
@Controller
@RequestMapping("/history")
@Slf4j
public class HistoryController {
    @Resource
    private PatientService patientService;
    @Resource
    private MedicalRecordService medicalRecordService;
    /**
     * 跳转门诊历史
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest request){
        Doctor doctor= (Doctor) request.getSession().getAttribute("doctor");
        model.addAttribute("doctor",doctor);
        return "main/history";
    }

    @RequestMapping("/medicalRecordsDateLists")
    @ResponseBody
    public Object medicalRecordsDateLists(String patient_id){
        Map<String,Object> resMap=new HashMap<>();
        Patient patient=patientService.queryById(patient_id);
        List<MedicalRecord> medicalRecords=medicalRecordService.queryByPatientId(patient_id);
        if(medicalRecords!=null){
            List<MedicalRecordDateDto> medicalRecordDateDtos=new ArrayList<>();
            for (MedicalRecord m:medicalRecords
            ) {
                MedicalRecordDateDto medicalRecordDateDto=new MedicalRecordDateDto();
                medicalRecordDateDto.setMedicalRecordDate(DateUtils.formateDate(m.getCreateTime()));
                medicalRecordDateDto.setMedicalRecordId(String.valueOf(m.getMedicalRecordId()));
                medicalRecordDateDtos.add(medicalRecordDateDto);
            }
            resMap.put("code","1");
            resMap.put("medicalRecordDateDtos",medicalRecordDateDtos);
            resMap.put("patient",patient);
            return resMap;
        }
        resMap.put("msg","网络连接错误，请稍后再试");
        return resMap;
    }
    @RequestMapping("/showMedicalRecord")
    @ResponseBody
    public Object showMedicalRecord(String medical_record_id,HttpServletRequest request){
        Map<String,Object> resMap=new HashMap<>();
        Doctor doctor= (Doctor) request.getSession().getAttribute("doctor");
        MedicalRecord medicalRecord=medicalRecordService.queryById(Integer.parseInt(medical_record_id));
        if(medicalRecord!=null){
            Patient patient=patientService.queryById(medicalRecord.getPatientId());
            resMap.put("code","1");
            resMap.put("symptom",medicalRecord.getSymptom());
            resMap.put("diagnosis",medicalRecord.getDiagnosis());
            resMap.put("treat", medicalRecord.getTreatmentOptions());
            resMap.put("date",DateUtils.formateDate(medicalRecord.getCreateTime()));
            resMap.put("patient",patient);
            return  resMap;
        }
        resMap.put("msg","网络连接错误，请稍后再试");
        return resMap;
    }
}
